﻿@inherits AdminCompontentBase
<div class="import-receivers" @ref="Ref">
    <SSimpleModal Value="@_visible" ValueChanged="HandleVisibleChanged" Title="@T("View")" BodyClass="mt-9 rounded-0">
        <ChildContent>
            <MSimpleTable Height="539" class="table-boder-head">
                <thead>
                    <tr>
                    @if (ChannelType == ChannelTypes.WebsiteMessage)
                    {
                        <th class="text-left body2 regular3--text rounded-0">
                            @T("UserId")
                        </th>
                    }
                    @if (ChannelType == ChannelTypes.Sms)
                    {
                        <th class="text-left body2 regular3--text rounded-0">
                            @T("PhoneNumber")
                        </th>
                    }
                    @if (ChannelType == ChannelTypes.Email)
                    {
                        <th class="text-left body2 regular3--text rounded-0">
                            @T("Email")
                        </th>
                    }
                    @if (ChannelType == ChannelTypes.App)
                    {
                        <th class="text-left body2 regular3--text rounded-0">
                            @T("ClientId")
                        </th>
                    }
                    @if (ChannelType == ChannelTypes.WeixinWork || ChannelType == ChannelTypes.WeixinWorkWebhook)
                    {
                        <th class="text-left body2 regular3--text rounded-0">
                            @T("Account")
                        </th>
                    }
                    @if (Value.Count > 0)
                    {
                        foreach (var item in Value[0].Variables)
                        {
                            <th class="text-left body2 regular3--text rounded-0">
                                @item.Key
                            </th>
                        }
                    }
                    </tr>
                </thead>
                <tbody>
                @foreach (var item in _items)
                {
                    <tr class="body2 regular--text mt-4 mb-6">
                        <td class="body2 regular--text border-bottom-none">@item.ChannelUserIdentity</td>
                    @foreach (var v in item.Variables)
                    {
                        <td class="body2 regular--text border-bottom-none">@v.Value</td>
                    }
                    </tr>
                }
                </tbody>
            </MSimpleTable>
            <SPagination Class="full-width" Page="@_queryParam.Page" PageSize="@_queryParam.PageSize" Total="Value.Count" PageChanged="HandlePageChanged" PageSizeChanged="HandlePageSizeChanged" />
        </ChildContent>
    </SSimpleModal>
</div>


@code {
    [Parameter]
    public List<MessageTaskReceiverDto> Value { get; set; } = new();

    [Parameter]
    public ChannelTypes? ChannelType { get; set; }

    private List<MessageTaskReceiverDto> _items { get; set; } = new();

    private bool _visible;
    private PaginatedOptionsDto _queryParam = new();

    private void HandleVisibleChanged(bool val)
    {
        if (!val) HandleCancel();
    }

    public async Task OpenModal()
    {
        _visible = true;
        await LoadData();
    }

    private void HandleCancel()
    {
        _visible = false;
        ResetForm();
    }

    private async Task HandlePageChanged(int page)
    {
        _queryParam.Page = page;
        await LoadData();
    }

    private async Task HandlePageSizeChanged(int pageSize)
    {
        _queryParam.PageSize = pageSize;
        await LoadData();
    }

    private async Task LoadData()
    {
        _items = Value.Skip((_queryParam.Page - 1) * _queryParam.PageSize).Take(_queryParam.PageSize <= 0 ? int.MaxValue : _queryParam.PageSize).ToList();
        await InvokeAsync(StateHasChanged);
    }

    private void ResetForm()
    {
        _queryParam = new();
    }
}
